/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 *
 * @author EnzoZhong
 */
public class Foto {

      public static Integer setXMid ( Integer paperWidth , Integer num ) {
            return ( paperWidth - num ) / 2;
      }

      /**
       *
       * @param image
       * @return
       */
      public static synchronized Double getDiametro ( BufferedImage image ) {
            Integer width = image.getWidth ();
            Integer height = image.getHeight ();
            Integer diametro = width * width + height * height;
            return Math.sqrt ( diametro );
      }

      /**
       *
       * @param image
       * @return
       */
      public static synchronized Border findBorder ( BufferedImage image ) {
            Integer[][] array = bufferedImage2IntegerArray ( image );
            Point min = new Point ();
            Point max = new Point ();

            for ( int x = 0 ; x < image.getWidth () ; x ++ ) {
                  Integer[] line = Num.getRow ( array , x );
                  if (  ! Num.allEpuale ( line , 0 ) ) {
                        min.x = x;
                        break;
                  }
            }
            for ( int y = 0 ; y < image.getHeight () ; y ++ ) {
                  Integer[] line = Num.getColumn ( array , y );
                  if (  ! Num.allEpuale ( line , 0 ) ) {
                        min.y = y;
                        break;
                  }
            }

            for ( int x = image.getWidth () - 1 ; x > 0 ; x -- ) {
                  Integer[] xLine = Num.getRow ( array , x );
                  if (  ! Num.allEpuale ( xLine , 0 ) ) {
                        max.x = x;
                        break;
                  }
            }
            for ( int y = image.getHeight () - 1 ; y > 0 ; y -- ) {
                  Integer[] xLine = Num.getColumn ( array , y );
                  if (  ! Num.allEpuale ( xLine , 0 ) ) {
                        max.y = y;
                        break;
                  }
            }

            return new Border ( min , max );
      }

      /**
       *
       * @param image
       *
       * @return
       */
      public static synchronized Integer[][] bufferedImage2IntegerArray ( BufferedImage image ) {
            Integer height = image.getHeight ();
            Integer width = image.getWidth ();
            Integer[][] array = new Integer[ height ][ width ];
            for ( int x = 0 ; x < image.getHeight () ; x ++ ) {
                  for ( int y = 0 ; y < image.getWidth () ; y ++ ) {
                        array[x][y] = image.getRGB ( y , x );
                  }
            }
            return array;
      }

      /**
       *
       */
      public static class Border {

            /**
             *
             */
            public Point min;
            /**
             *
             */
            public Point max;

            /**
             *
             * @param min
             * @param max
             */
            public Border ( Point min , Point max ) {
                  this.min = min;
                  this.max = max;
            }

            @Override
            public boolean equals ( Object obj ) {
                  if ( obj == null ) {
                        return false;
                  }
                  if ( getClass () != obj.getClass () ) {
                        return false;
                  }
                  final Border other = ( Border ) obj;
                  if ( this.min != other.min && ( this.min == null ||  ! this.min.equals ( other.min ) ) ) {
                        return false;
                  }
                  if ( this.max != other.max && ( this.max == null ||  ! this.max.equals ( other.max ) ) ) {
                        return false;
                  }
                  return true;
            }

            @Override
            public int hashCode () {
                  int hash = 3;
                  hash = 53 * hash + ( this.min != null ? this.min.hashCode () : 0 );
                  hash = 53 * hash + ( this.max != null ? this.max.hashCode () : 0 );
                  return hash;
            }

            @Override
            public String toString () {
                  return "Border{" + "min=" + min + ", max=" + max + '}';
            }

      }

      public static class Height {

            private Integer start;
            private Integer startHeight;
            private Integer interval;

            public Height ( Integer startHeight , Integer interval ) {
                  this.start = startHeight;
                  this.startHeight = startHeight;
                  this.interval = interval;
            }

            public static Height born ( Integer startHeight , Integer interval ) {
                  return new Height ( startHeight , interval );
            }

            public Integer add ( Integer addHeight ) {
                  Integer nowHeight = startHeight;
                  startHeight = startHeight + addHeight + interval;
                  return nowHeight;
            }

            public void reset () {
                  this.startHeight = start;
            }

      }

}
